package com.service.abolishing.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.abolishing.entity.LocationResult;
import com.service.abolishing.rabbitmq.MsgProducer;
import com.service.abolishing.service.LocationResultService;
import com.service.abolishing.service.UserLocationLogService;
import com.service.abolishing.task.ScheduledService;
import com.service.config.dto.MailBean;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("location")
@Api(value = "定位数据接口", tags = "定位数据接口")
public class LocationResultController {
    @Resource
    LocationResultService locationResultService;
    @Resource
    RedisUtil redisUtil;
    @Resource
    MsgProducer msgProducer;
    @Resource
    ScheduledService scheduledService;
    /**
     * 服务对象
     */
    @Resource
    private UserLocationLogService userLocationLogService;
    /**
     * 查询历史或者全部
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询历史")
    @RequestMapping(value = "/lbs/historyLocation",method = RequestMethod.GET)
    @ResponseBody
    public Result historyLocation(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                  @RequestParam (value = "pageSize",required = false) @ApiParam("条数")Integer pageSize,
                                  @RequestParam (value = "pageNum",required = false) @ApiParam("页码")Integer pageNum,
                                  @RequestParam (value = "terminalMac",required = false ) @ApiParam("终端mac")String terminalMac,
                                  @RequestParam (value = "startDate",required = false ) @ApiParam("开始日期")String startDate,
                                  @RequestParam (value = "endDate",required = false ) @ApiParam("开始日期")String endDate){
        PageHelper.startPage(pageNum,pageSize);
        List<LocationResult> locationResultList = new ArrayList<>();
        if (!terminalMac.isEmpty())
            //mark标志位 表示如果需要查询更多历史  则需要去查询数据库，则mark为1
            locationResultList = locationResultService.queryByTerminalMac("", startDate, endDate);

        /*脱敏 */
        locationResultList.forEach(locationResult -> {
            if (locationResult.getTerminalMac().equals(terminalMac)){
                locationResult.setUname("本机");
            }else {
                String Uname = StringUtils.getStarString(locationResult.getTerminalMac().trim().replace(":",""),2,10);
                locationResult.setUname(Uname);
            }
        });
        PageInfo<LocationResult> pageInfo = new PageInfo<>(locationResultList);//分页
        HashMap<Object, Object> map= new HashMap<>(16);
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("locationResultList",pageInfo.getList());
        return Result.ok().setData(map);
    }
    /**
     * 查询个人历史
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询个人历史")
    @RequestMapping(value = "/lbs/personalHistoryLocation",method = RequestMethod.GET)
    @ResponseBody
    public Result personalHistoryLocation(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                          @RequestParam (value = "pagesize",required = false) @ApiParam("条数")Integer pagesize,
                                          @RequestParam (value = "pageNum",required = false) @ApiParam("页码")Integer pageNum,
                                          @RequestParam (value = "terminalMac",required = false )@ApiParam("终端mac") String terminalmac,
                                          @RequestParam (value = "startDate",required = false )@ApiParam("开始日期") String startDate,
                                          @RequestParam (value = "endDate",required = false ) @ApiParam("结束日期")String endDate) throws Exception {
        List<LocationResult> locationResultList = new ArrayList<>();
        if (!terminalmac.isEmpty()) {
            locationResultList = locationResultService.queryByTerminalMac(terminalmac, startDate, endDate);
        }
        /*脱敏 */
        locationResultList.forEach(locationResult -> {
            String Uname = StringUtils.getStarString(locationResult.getTerminalMac().trim().replace(":",""),2,10);
            locationResult.setUname(Uname);
        });
        return Result.ok().setData(locationResultList);
    }
    /**
     * 查询个人历史
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询测试")
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    @ResponseBody
    public Result test() throws Exception {

        MailBean mailBean = new MailBean();
        mailBean.setContent("3");
        msgProducer.sendMsg(mailBean,1);
        scheduledService.getLoginUserDispose1();
        return Result.ok();
    }
    /**
     * 第三方调用
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "第三方调用")
    @RequestMapping(value = "/lbs/gethistoryLocation",method = RequestMethod.GET)
    @ResponseBody
    public Result gethistoryLocation(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                     @RequestParam @ApiParam("开始日期")String startDate,
                                     @RequestParam @ApiParam("结束日期")String endDate,
                                     @RequestParam (value = "terminalMac",required = false )@ApiParam("终端mac") String terminalmac) throws Exception {
        List<LocationResult> locationResultList = new ArrayList<>();
        if (terminalmac != null) {
            locationResultList = locationResultService.getLocationResultByMac(terminalmac);
        }else {
            locationResultList =  locationResultService.getHistoryLocation(startDate, endDate);
        }
        return Result.ok().setData(locationResultList);
    }
}
